<template>
    <a-card :bordered="false">
        <div class="table-operator">
            <a-button v-if="hasPerm('aa_rateset.Add')" icon="plus" type="primary" @click="handleEdit()">新建</a-button>
        </div>
        <!-- <div class="table-page-search-wrapper">
          <a-form layout="inline">
            <a-row :gutter="48">
              <a-col :md="6" :sm="24">
                <a-form-item label="角色名">
                  <a-input v-model="queryParam.roleName" placeholder @keydown.enter="getDataList" />
                </a-form-item>
              </a-col>
              <a-col :md="6" :sm="24">
                <a-button
                  type="primary"
                  @click="() => {
                          this.pagination.current = 1
                          this.getDataList()
                      }
                      "
                  >查询
                </a-button>
                <a-button style="margin-left: 8px" @click="queryParam = {}; getDataList()">重置</a-button>
              </a-col>
            </a-row>
          </a-form>
        </div> -->
        <a-table ref="table" :bordered="true" :columns="columns" :dataSource="data" :loading="loading"
            :pagination="pagination" :rowKey="(row, index) => row.ID" :scroll="{ y: 530 }" size="small"
            @change="handleTableChange">
            <span slot="index" slot-scope="text, record,i">{{ (pagination.current - 1) * pagination.pageSize + i + 1
            }}</span>
            <span slot="action" slot-scope="text, record">
                <template>
                    <a @click="handleEdit(record.Id)">编辑</a>
                    <a-divider v-if="hasPerm('aa_rateset.Edit') && hasPerm('aa_rateset.Delete')" type="vertical" />
                    <a v-if="hasPerm('aa_rateset.Delete')" @click="handleDelete([record.Id])">删除</a>
                </template>
            </span>
        </a-table>
        <a-drawer title="编辑" placement="right" :visible="visible" @close="onClose" width="20%">
            <a-form-model :model="entity">
                <a-form-item label="主单位" style="display: flex;">
                    <a-input disabled style="width:50%;" v-model="entity.MainName"></a-input>
                    <a-button type="primary" @click="handleOpenChoose('main')">
                        <a-icon type="search" />
                    </a-button>
                </a-form-item>
                <a-form-item label="辅单位" style="display: flex;">
                    <a-input disabled style="width:50%;" v-model="entity.SubName"></a-input>
                    <a-button type="primary" @click="handleOpenChoose('sub')">
                        <a-icon type="search" />
                    </a-button>
                </a-form-item>
                <a-form-item label="换算率" style="display: flex;">
                    <a-input oninput="value=value.replace(/^\D*(\d*(?:\.\d{0,})?).*$/g, '$1')" style="width:50%;"
                        v-model="entity.ChangeRate"></a-input>
                </a-form-item>
            </a-form-model>
            <div :style="{
                position: 'absolute',
                right: 0,
                bottom: 0,
                width: '100%',
                borderTop: '1px solid #e9e9e9',
                padding: '10px 16px',
                background: '#fff',
                textAlign: 'right',
                zIndex: 1,
            }">
                <a-button :style="{ marginRight: '8px' }" @click="onClose">取消</a-button>
                <a-button type="primary" @click="handleSubmit">保存</a-button>
            </div>
        </a-drawer>
        <Unit ref="Unit" @onChoose="handleChoose" />
    </a-card>
</template>

<script>
import Token from '@/utils/cache/TokenCache'
import moment from 'moment'
import { download } from '@/components/download'
import Unit from "./Unit.vue"
const columns = [
    {
        title: '编号',
        dataIndex: 'index',
        scopedSlots: { customRender: 'index' }
    },
    { title: '主单位', dataIndex: 'MainName', },
    { title: '辅单位', dataIndex: 'SubName', },
    { title: '换算率', dataIndex: 'ChangeRate' },
    { title: '操作', dataIndex: 'action', align: 'left', width: '100px', scopedSlots: { customRender: 'action' }, }
]

export default {
    components: {
        Token,
        Unit
    },
    mounted() {
        this.$nextTick(() => {
            // 页面渲染完成后的回调
            // this.tableHeight = this.getTableHeight(true) + 24
            this.time = [this.timeFormat(new Date(new Date().getFullYear(), new Date().getMonth(), 1)), this.timeFormat(new Date(new Date().getFullYear(), new Date().getMonth() + 1, 0))]
            this.getDataList()
        })
    },
    data() {
        return {
            entity: {},
            headers: {
                authorization: 'Bearer ' + Token.getToken()
            },
            data: [],
            visible: false,
            pagination: {
                current: 1,
                pageSize: 25,
                showTotal: (total, range) => `总数:${total} 当前:${range[0]}-${range[1]}`,
                pageSizeOptions: ['25', '50', '100'], // 设置pageSize的可选值，页面可以通过下拉框进行选择
                showSizeChanger: true
            },
            filters: {},
            sorter: { field: 'dDate', order: 'desc' },
            loading: false,
            columns,
            queryParam: { condition: 'code' },
            visibleUpload: false,
            selectedRowKeys: [],
            time: [],
            timeDisabled: true,
            mainOptions: [],
            subOptions: [],
        }
    },
    methods: {
        repossess() {
            this.queryParam = { condition: 'code' }
            this.time = [];
        },
        handleTableChange(pagination, filters, sorter) {
            this.pagination = { ...pagination }
            this.filters = { ...filters }

            this.getDataList()
        },
        handleSubmit() {
            this.$http.post('/InvoiceManagement/aa_rateset/SaveData', this.entity).then(res => {
                if (res.Success) {
                    this.$message.success('提交成功!')
                    this.visible = false;
                    this.getDataList();
                }
            }).catch(err => {
                this.$message.error(err)
            })
        },
        onSelectChange(selectedRowKeys) {
            this.selectedRowKeys = selectedRowKeys
        },
        getDataList() {
            this.selectedRowKeys = []

            this.loading = true
            this.$http
                .post('/InvoiceManagement/aa_rateset/GetDataList', {
                    PageIndex: this.pagination.current,
                    PageRows: this.pagination.pageSize,
                    SortField: 'Id',
                    SortType: 'desc',
                    // Search: { ...this.queryParam, StartTime: this.time[0] ? this.timeFormat(this.time[0]) : "", EndTime: this.time[1] ? this.timeFormat(this.time[1]) : "" },
                })
                .then(resJson => {
                    this.loading = false
                    this.data = resJson.Data
                    if (this.data) {
                        const pagination = { ...this.pagination }
                        pagination.total = resJson.Total
                        this.pagination = pagination
                    }
                }).catch(err => {
                    this.$message.error(err);
                })
        },
        onClose() {
            this.visible = false;
        },
        hasSelected() {
            return this.selectedRowKeys.length > 0
        },
        handleEdit(id) {
            this.visible = true;
            this.entity = {};
            if (id) {
                this.title = '编辑'
                this.$http.post('/InvoiceManagement/aa_rateset/GetTheData', { id }).then(res => {
                    this.entity = res.Data;
                })
            } else {
                this.title = '新建'
            }
        },

        handleDelete(ids) {
            var thisObj = this
            this.$confirm('确认删除吗?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
                thisObj.$http
                    .post('/InvoiceManagement/aa_rateset/DeleteData', ids)
                    .then(resJson => {
                        // resolve()

                        if (resJson.Success) {
                            thisObj.$message.success('操作成功!')

                            thisObj.getDataList()
                        } else {
                            thisObj.$message.error(resJson.Msg)
                        }
                    })
            })
        },
        handleOpenChoose(type) {
            this.$refs.Unit.openChoose(type)
        },
        handleChoose(selectedRows, type) {
            console.log(selectedRows[0].name)
            if (type == 'main') {
                this.mainOptions = selectedRows;
                this.$set(this.entity, 'MainId', selectedRows[0].id)
                this.$set(this.entity, 'MainName', selectedRows[0].name)
            } else {
                this.subOptions = selectedRows;
                this.$set(this.entity, 'SubId', selectedRows[0].id)
                this.$set(this.entity, 'SubName', selectedRows[0].name)
            }
        },
    }
}
</script>

<style scoped>
.flax {
    display: flex;
    justify-content: space-between;
}

/* .download {
  float: right;
  display: flex;
  align-items: center;
} */
</style>